import { __private, EventTarget } from "cc";
import { EDITOR } from "cc/env";
import { input } from "cc";
class InputEventDispatcher {
    public priority = 10086;
    private _inputEventTarget: EventTarget;

    constructor(inputEventTarget: EventTarget) {
        this._inputEventTarget = inputEventTarget;
    }

    public dispatchEvent(event: Event): boolean {
        this._inputEventTarget.emit(event.type, event);
        return true;
    }
}

// @siclass('CocosGlobalInput')
export default class CocosGlobalInput {

    private static ins: CocosGlobalInput;

    static getInstance() {
        if (!this.ins) {
            this.ins = new CocosGlobalInput();
        }
        return this.ins;
    }
    public static destroyInstance() {
        if (this.ins) {
            this.ins = null;
        }
    }

    private _eventTarget: EventTarget = new EventTarget();
    private _inputEventDispatcher: InputEventDispatcher;

    constructor() {
        this.init();
    }
    init() {

        if (!this._inputEventDispatcher) {
            this._inputEventDispatcher = new InputEventDispatcher(this._eventTarget);
            //@ts-ignore
            input._registerEventDispatcher(this._inputEventDispatcher);
            // this.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);

        }

    }
    /**
     * @en
     * Register a callback of a specific input event type.
     * @zh
     * 注册特定的输入事件回调。
     *
     * @param eventType - The event type
     * @param callback - The event listener's callback
     * @param target - The event listener's target and callee
     */
    public on<K extends keyof __private._cocos_input_input__InputEventMap>(eventType: K, callback: __private._cocos_input_input__InputEventMap[K], target?: any) {
        if (EDITOR) {
            return callback;
        }
        this._eventTarget.on(eventType, callback, target);
        return callback;
    }
    /**
     * @en
     * Register a callback of a specific input event type.
     * @zh
     * 注册特定的输入事件回调。
     *
     * @param eventType - The event type
     * @param callback - The event listener's callback
     * @param target - The event listener's target and callee
     */
    public off<K extends keyof __private._cocos_input_input__InputEventMap>(eventType: K, callback: __private._cocos_input_input__InputEventMap[K], target?: any) {
        if (EDITOR) {
            return callback;
        }
        this._eventTarget.off(eventType, callback, target);
        return callback;
    }
}